﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WhittleList.Bll
{
    public partial class UserProjectRole : IValidate
    {
        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (!IsValid)
                throw new Exceptions.ValidationException("Rule Violation Prevented Saving", GetRuleViolations());
        }
        #region IValidate Members

        public bool IsValid
        {
            get { return GetRuleViolations().Count() == 0; }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (ProjectId <= 0)
                yield return new RuleViolation("ProjectId must be set", "ProjectId");
            if (UserId <= 0)
                yield return new RuleViolation("UserId must be set", "UserId");
            yield break;
        }

        #endregion

        internal void OnInserting(User currentUser)
        {
            this._LastUpdatedyId = this._CreatedById = currentUser.Id;
            this._UTCLastUpdated = this._UTCCreated = DateTime.UtcNow;
        }

        internal void OnUpdating(User currentUser)
        {
            this._LastUpdatedyId = currentUser.Id;
            this._UTCLastUpdated = DateTime.UtcNow;
        }
    }
}
